package com.ahui.ydd.controller;

import com.ahui.ydd.dto.YddAdvposDto;
import com.ahui.ydd.common.R;
import com.ahui.ydd.entity.YddAdvimg;
import com.ahui.ydd.entity.YddAdvpos;
import com.ahui.ydd.page.YddAdvposPage;
import com.ahui.ydd.service.YddAdvimgService;
import com.ahui.ydd.service.YddAdvposService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advpos")
@Slf4j
public class YddAdvposController {
    @Autowired
    private YddAdvposService yddAdvposService;

    @Autowired
    private YddAdvimgService yddAdvimgService;

    /**
     * 广告位列表分页查询
     * @param yddAdvposDto
     * @return
     */
    @GetMapping("/list")
    public R<YddAdvposPage> list(YddAdvposDto yddAdvposDto){
        //分页拦截器
        Page<YddAdvpos> page =new Page<>(yddAdvposDto.getPage(),yddAdvposDto.getLimit());
        //条件构造器
        LambdaQueryWrapper<YddAdvpos> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(yddAdvposDto.getAdvposname()),YddAdvpos::getAdvposname,yddAdvposDto.getAdvposname());
        queryWrapper.like(StringUtils.isNotEmpty(yddAdvposDto.getAdvposdesc()),YddAdvpos::getAdvposdesc,yddAdvposDto.getAdvposdesc());
        //查询
        yddAdvposService.page(page,queryWrapper);
        //数据复制
        Page<YddAdvposDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(page,dtoPage,"recods");
        List<YddAdvposDto> list = page.getRecords().stream().map((yddAdvpos) -> {
            YddAdvposDto dto = new YddAdvposDto();
            BeanUtils.copyProperties(yddAdvpos, dto);
            LambdaQueryWrapper<YddAdvimg> yddAdvimgLambdaQueryWrapper = new LambdaQueryWrapper<>();
            yddAdvimgLambdaQueryWrapper.eq(YddAdvimg::getAdvimgpos, yddAdvpos.getId());
            int count = yddAdvimgService.count(yddAdvimgLambdaQueryWrapper);
            dto.setAdvposnum(count);
            return dto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        //数据返回
        YddAdvposPage yddAdvposPage =new YddAdvposPage();
        yddAdvposPage.setCount(dtoPage.getTotal());
        yddAdvposPage.setCurrentPage(dtoPage.getCurrent());
        yddAdvposPage.setData(dtoPage.getRecords());
        yddAdvposPage.setPageSize(dtoPage.getSize());
        yddAdvposPage.setTotalPages(dtoPage.getPages());
        return R.success(yddAdvposPage);
    }

    /**
     * 广告位修改页面回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddAdvpos> rowInfo(int id){
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        return R.success(yddAdvpos);
    }

    /**
     * 广告位修改
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvpos yddAdvpos){
        yddAdvposService.save(yddAdvpos);
        return R.success("修改成功");
    }

    /**
     *广告位删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> del(int id){
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddAdvimg::getAdvimgpos,id);
        List<YddAdvimg> list = yddAdvimgService.list(queryWrapper);
        if(list!=null){
            return R.error("请先删除关联广告图");
        }
        yddAdvposService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 广告位列表-全部
     * @return
     */
    @GetMapping("/all")
    public R<List<YddAdvpos>> all(){
        List<YddAdvpos> list = yddAdvposService.list();
        return R.success(list);
    }

    /**
     * 广告位添加
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody YddAdvpos yddAdvpos){
        yddAdvposService.save(yddAdvpos);
        return R.success("添加成功");
    }
}
